﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Scan.Controls
{
    public partial class MessageBoxEx : Form
    {
        public enum BeepType
        {
            SimpleBeep = -1,
            IconAsterisk = 0x00000040,
            IconExclamation = 0x00000030,
            IconHand = 0x00000010,
            IconQuestion = 0x00000020,
            Ok = 0x00000000,
        }

        [DllImport("coredll.dll")]
        public static extern bool MessageBeep(BeepType beepType);

        private static MessageBoxEx msgBx = null;

        public MessageBoxEx()
        {
            InitializeComponent();
        }

        public MessageBoxEx(string msg, string title, MessageBoxButtons btn, MessageBoxIcon icon, MessageBoxDefaultButton defaultBtn)
        {
            InitializeComponent();
            this.lbCaption.Text = title;
            this.lbMsg.Text = msg;
            if (btn == MessageBoxButtons.YesNo)
            {
                this.btnConfirm.Visible = true;
                this.btnCanncel.Visible = true;
            }
            else
            {
                this.btnConfirm.Visible = false;
                this.btnCanncel.Visible = false;
            }
            if (icon == MessageBoxIcon.Question)
            {
                this.lbMsg.ForeColor = Color.Red;
            }
            else
            {
                this.lbMsg.ForeColor = Color.Black;
            }
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Yes;
        }

        private void btnCanncel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
        }

        public static DialogResult Show(string msg, string title, MessageBoxButtons btn, MessageBoxIcon icon, MessageBoxDefaultButton defaultBtn)
        {
            msg = (msg == null) ? "" : msg;
            title = (title == null) ? "" : title;
            if (msgBx == null)
            {
                msgBx = new MessageBoxEx(msg, title, btn, icon, defaultBtn);
            }
            msgBx.lbCaption.Text = title;
            msgBx.lbMsg.Text = msg;
            
            if (defaultBtn == MessageBoxDefaultButton.Button1)
            {
                msgBx.btnConfirm.Focus();
            }
            else
            {
                msgBx.btnCanncel.Focus();
            }
            if (icon == MessageBoxIcon.Question)
            {
                MessageBeep(BeepType.IconQuestion);
            }
            else
            {
                MessageBeep(BeepType.IconAsterisk);
            }
            msgBx.Location = new Point(20, 50);
            return msgBx.ShowDialog();
        }

        public static DialogResult Show(string msg, string title)
        {
            msg = (msg == null) ? "" : msg;
            title = (title == null) ? "" : title;
            if (msgBx == null)
            {
                msgBx = new MessageBoxEx(msg, title, MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
            }
            msgBx.lbCaption.Text = title;
            msgBx.lbMsg.Text = msg;
            MessageBeep(BeepType.IconAsterisk);
            msgBx.Location = new Point(20, 50);
            return msgBx.ShowDialog();
        }
    }
}